<template>
  <div class="comment">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="v in list" :key="v.aut_id.toString()">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="v.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ v.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ v.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ v.pubdate | relative }}</span>
            <van-button size="mini" type="default" class="res_btn" @click="reply(v)">
              <span v-show="v.reply_count">{{ v.reply_count }}</span>
              回复
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="v.is_liking ? 'like' : 'like-o'"
          @click="toggleCommentLike(v)"
        />
        <span class="icon_count" v-show="v.like_count">{{ v.like_count }}</span>
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 弹出层 -->
    <van-popup v-model="show" position="bottom" :style="{ height: '70%' }" round>
      <comment-reply
        :commentReply="commentReply"
        :art_id="art_id"
        @close="show = false"
        v-if="show"
      ></comment-reply>
    </van-popup>
    <!-- /弹出层 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
        @keyup.enter="addComment"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import { getComments, postComments, postLikings, delLikings } from '@/api/comment'
import commentReply from './commentReply.vue'

export default {
  components: { commentReply },
  props: ['art_id'],
  data() {
    return {
      list: [], // 搜索结果
      loading: false, // 是否正在加载
      finished: false, // 是否加载完毕
      offset: null,
      content: '', // 发布评论
      show: false, // 弹出层显示和隐藏
      commentReply: {}, // 需要回复的那一项
    }
  },
  methods: {
    // 一、加载评论
    async onLoad() {
      // 1.1 发送请求,保存数据
      const { data } = await getComments(this.art_id, this.offset)
      // console.log(data)
      const arr = data.data.results
      this.list = this.list.concat(arr)
      // 1.2 加载状态结束
      this.loading = false
      // 1.3 数据全部加载完成
      if (arr.length === 0) {
        this.finished = true
      }
      // 1.4 更新offset
      this.offset = data.data.last_id
    },

    // 二、发布评论
    async addComment() {
      // 2.1 非空校验
      if (!this.content) return
      // 2.2 发送请求,更新视图,将评论添加到list的最前面
      try {
        const { data } = await postComments(this.art_id, this.content)
        // 解决发布新评论后无法点赞的问题
        data.data.new_obj.is_liking = false
        this.list.unshift(data.data.new_obj)
        // 2.3 清空内容,提示
        this.content = ''
        this.$toast.success('发布成功')
      } catch {
        this.$toast.fail('该文章禁止评论')
        this.content = ''
      }
    },

    // 三、对评论点赞/取消点赞
    async toggleCommentLike(v) {
      // console.log(v)
      // console.log(this.$refs.vanIcon)
      try {
        // 3.1 判断当前是否已经点赞 is_liking:false 表示没点赞
        if (!v.is_liking) {
          // 值为false 发送点赞请求
          await postLikings(v.com_id.toString())
          this.$toast.success('点赞')
          v.like_count++
        } else {
          // 值为true 发送取消点赞请求
          await delLikings(v.com_id.toString())
          this.$toast.success('取消点赞')
          v.like_count--
        }
        // 3.2 根据评论id找到对应数据,修改视图
        const i = this.list.findIndex(item => item.com_id === v.com_id)
        this.list[i].is_liking = !this.list[i].is_liking
        // console.log(i)
      } catch {
        this.$toast.success('操作失败')
      }
    },

    // 四、点击回复显示弹出层
    reply(value) {
      // console.log(value)
      this.commentReply = value
      this.show = true
    },
  },
  mounted() {},
}
</script>

<style lang="scss" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
.res_btn {
  top: -4px;
}
.van-icon-like {
  color: red;
}
.van-info {
  background-color: white;
  color: #333;
  top: 7px;
  right: 24px;
}
.icon_count {
  position: relative;
  color: #333;
  font-size: 12px;
  top: -5px;
  // left: -4px;
  padding-right: 4px;
}
</style>
